package com.example.sjjg.utest

import android.os.Bundle
import androidx.fragment.app.testing.launchFragment
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.lifecycle.Lifecycle
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.sjjg.utest.view.DataFragment
import com.example.sjjg.utest.view.MainFragment
import com.example.sjjg.utest.view.ResultFrgmt
import com.google.common.truth.Truth
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class RobolectricFragment {
    fun robolectric_test_fragment(){
//        Robolectric.buildFragment(MainFragment::class.java)
    }
}

@RunWith(AndroidJUnit4::class)
class AndroidXTestFragment{

    @Test
    fun frgmt_test1(){

        // GIVEN
        val args = Bundle().apply {
            putString("name","test")
            putString("class","05")
            putInt("age",18)
            putString("email","hi@test.com")
        }

        // WHEN
        //This method cannot be called from the main thread
        val scenario = launchFragment<MainFragment>(
            fragmentArgs = args,
            themeResId   = R.style.FragmentScenarioEmptyFragmentActivityTheme,
            initialState = Lifecycle.State.RESUMED,
            factory      = null) //null to use default factory

        // THEN
        scenario.onFragment{ mainFrgmt ->
            Truth.assertThat(mainFrgmt.lifeCycle > 0)
            assertTrue(mainFrgmt.map["name"] == "test")
        }
    }

    @Test
    fun frgmt_test2(){

        // GIVEN
        val args = Bundle()
        args.putInt("value",2)

        // WHEN
        val scenario = launchFragmentInContainer<MainFragment>(args)
        scenario.moveToState(Lifecycle.State.CREATED)

        // THEN
        scenario.onFragment{ mainFrgmt ->
            Truth.assertThat(mainFrgmt.lifeCycle == 2)
        }
    }
    @Test
    fun frgmt_test3(){

        // GIVEN
        val txt = "result"

        // WHEN
        val scenario = launchFragmentInContainer<ResultFrgmt>()

        // THEN
        scenario.onFragment{ fragment ->
            fragment.parentFragmentManager.setFragmentResultListener("requestKey",fragment.viewLifecycleOwner) { key, bundle ->
                val result : String? = bundle.getString("resultKey")
                Truth.assertThat(result === txt)
            }
        }
    }
    @Test
    fun test_mvvm(){

        // GIVEN
        val mock = ArrayList<Int>().apply {
            for (i in 0..15){
                add(i)
            }
        }
        // WHEN
        val scenario = launchFragmentInContainer<DataFragment>()
        scenario.moveToState(Lifecycle.State.RESUMED)

        // THEN
        scenario.onFragment{ fragment ->
            for (i in mock.indices){
                fragment.viewModel.addData(i)
            }
            assertTrue(fragment.viewModel.data().size  == mock.size)
        }
    }
}